#include "httpreq.h"

int wifiok=0;

char * lastdata=0;

void initWifi(){

	if(wifiok == 1)
		return;

	// Intenta conectarte al AP...
	//iprintf("Conectando via WFC...\n\n");
	swiWaitForVBlank();

	if (NF_WiFiConnectDefaultAp()) {

		char txtbuff[32];

	
		sprintf(txtbuff,"Ip : %s", inet_ntoa(NF_IP));
		NF_WriteText(0, 0, 0, 0, txtbuff);
		sprintf(txtbuff,"Gateway : %s", inet_ntoa(NF_GATEWAY));
		NF_WriteText(0, 0, 0, 1, txtbuff);
		sprintf(txtbuff,"Mask : %s", inet_ntoa(NF_MASK));
		NF_WriteText(0, 0, 0, 2, txtbuff);
		sprintf(txtbuff,"Dns1 : %s", inet_ntoa(NF_DNS1));
		NF_WriteText(0, 0, 0, 3, txtbuff);
		sprintf(txtbuff,"Dns2 : %s", inet_ntoa(NF_DNS2));
		NF_WriteText(0, 0, 0, 4, txtbuff);
		NF_UpdateTextLayers();
	
		wifiok = 1;

	} else {

		NF_WriteText(0, 0, 0, 20, "Errer de connexion");
		NF_UpdateTextLayers();

	}

	swiWaitForVBlank();
	//return status;
}

int testget(){
	const char url[] = "http://jpprade.free.fr/ogserv/output.html";
	int err, i;
	sBuffer bfr;
	
	defaultExceptionHandler();
	
	
	ky_InitBuffer(&bfr);	
	err = ky_GetUrl(url, &bfr);
	
	char txtbuff[64];
	if (err < 0) {
		
		sprintf(txtbuff,"Error: %d, %d, %d | %d: %s\n", err, ky_errno, ky_h_errno, errno, strerror(errno));
		NF_WriteText(0, 0, 0, 5, txtbuff);
		NF_UpdateTextLayers();
	} else {

		char * tmpbuf = (char *) bfr.buffer;
		char * buf = (char*) malloc( (strlen(tmpbuf) + 10) * sizeof(char));
			/*Page * p = new Page;
			p->addContent(bfr.buffer);
			p->cleanEOL();
			p->parse();
			PA_OutputText(1, 2, 1, "recu : %s",p->getBody());*/
		
		sprintf(buf,"recu : %s",tmpbuf);
		NF_WriteText(0, 0, 0, 20, buf);
		NF_UpdateTextLayers();
		free(buf);
		
	}
	ky_FreeBuffer(&bfr);
	return err;
}

void loginview(char * uni,char * dom, char * login,char * pwd){
	char * url = (char*) malloc((68 + strlen(uni) + strlen(dom) + strlen(login) + strlen(pwd) + 1) * sizeof(char));
	//sprintf(url,"http://jpprade.free.fr/ogserv/testlogin.php?uni=%s&region=%s&login=%s&pwd=%s",uni,dom,login,pwd);
	sprintf(url,"http://dsdev.host56.com/testlogin.php?uni=%s&region=%s&login=%s&pwd=%s",uni,dom,login,pwd);
	//sprintf(url,"http://jpprade.free.fr/ogserv/testlogin.php?uni=%s&region=%s&login=%s&pwd=%s",uni,dom,login,pwd);
	//PA_Print(1, "%s\n",url);
	int res = getUrl(url);
	if(res == 0){
		free(url);
		return;
	}
	if(res < 0){
		NF_WriteText(0, 0, 0, 20, "retrying 1");
		NF_UpdateTextLayers();
		res = getUrl(url);
		if(res == 0){
			free(url);
			return;
		}
		NF_WriteText(0, 0, 0, 20, "double fail");
		NF_UpdateTextLayers();
		free(url);
	}	
}


int getUrl(char * url){
	int err, i;
	sBuffer bfr;
	initWifi();
	
	
	ky_InitBuffer(&bfr);
	err = ky_GetUrl(url, &bfr);
	
	if (err < 0) {
		char txtbuff[64];
		sprintf(txtbuff,"Error: %d, %d, %d | %d: %s\n", err, ky_errno, ky_h_errno, errno, strerror(errno));
		NF_WriteText(0, 0, 0, 5, txtbuff);
		NF_UpdateTextLayers();
		if( err != -23)
			ky_FreeBuffer(&bfr);
		return err;
	} else {
		char * tmpbuf = (char *) bfr.buffer;
		char * buf = (char*) malloc( (strlen(tmpbuf) + 10) * sizeof(char));
		//sprintf(buf,"recu : %s",tmpbuf);
		sprintf(buf,"%s",tmpbuf);
		//strcpy(buf,tmpbuf);
		lastdata = buf;
		//writedata();
		NF_WriteText(0, 0, 0, 5, buf);
		NF_WriteText(0, 0, 0, 20, "OK 1          ");
		NF_UpdateTextLayers();
		//free(buf);
		ky_FreeBuffer(&bfr);
	}
	return err;
}



void writedata(){
	int l ;
	FILE* file_id;
	file_id = fopen("savedata.bin", "w+");
	l = strlen(lastdata);
	if (file_id != NULL){
		NF_WriteText(0, 0, 0, 21, "write ok  ");
		NF_UpdateTextLayers();
		fseek(file_id, 0, SEEK_SET);                           // seek the the file start
		fwrite(lastdata,l , 1, file_id);      // read all the data into the account
		fclose(file_id);                                    // close the file
      }else{
		NF_WriteText(0, 0, 0, 21, "write not ok");
		NF_UpdateTextLayers();
	}
}